package io.bluerain.otherview.ui;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.ImageView;
import android.widget.SeekBar;

import io.bluerain.otherview.R;

/**
 * Created by foredawn on 15-9-17.
 * SeekBar基本用法
 */
public class SeekBarActivity extends Activity {
    static int IMG_ALPHA = 0x1;
    int imgId = R.drawable.img;
    SeekBar seekBar;
    ImageView img;
    Handler handler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.seekbar_activity);
        seekBar = (SeekBar) findViewById(R.id.seekBar);
        img = (ImageView) findViewById(R.id.img_seek);
        img.setImageResource(imgId);
        handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                if (msg.what == IMG_ALPHA) {
                    Bundle bundle = msg.getData();
                    int alpha = bundle.getInt("alpha");
                    img.setImageAlpha(alpha);
                }
            }
        };
        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                Message msg = new Message();
                Bundle bundle = new Bundle();
                bundle.putInt("alpha", progress);
                msg.setData(bundle);
                msg.what = IMG_ALPHA;
                handler.sendMessage(msg);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
    }
}
